package com.osell.selection.ui.main;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.da.lon.wang.xlg.R;
import com.osell.selection.data.network.RestAPI;
import com.osell.selection.ui.base.BaseFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HomeFragment extends BaseFragment {

    @BindView(R.id.tab_products)
    TabLayout tab_products;
    @BindView(R.id.vp_products)
    ViewPager vp_products;

    private Unbinder mUnBinder;

    private Disposable mDisposable;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        mUnBinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        tab_products.setupWithViewPager(vp_products);
        tab_products.setSelectedTabIndicatorColor(ContextCompat.getColor(view.getContext(), R.color.colorPrimary));

        mDisposable = RestAPI.getInstance().oSellService().getCategoriesProducts(21, null, 1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(homeInfoResponseData -> vp_products.setAdapter(new HomeProductsPagerAdapter(getChildFragmentManager(), homeInfoResponseData.data.categories)),
                        throwable -> {
                        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mDisposable.dispose();
        mUnBinder.unbind();
    }
}
